package com.cyz.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.admin.service.StudyResourceTypeService;
import com.cyz.common.bean.StudyResourceType;
import com.cyz.common.dto.StudyResourceTypeDto;
import com.cyz.common.enums.MyEnums;
import com.cyz.common.mapper.StudyResourceTypeMapper;
import com.cyz.common.utils.BeanCopyUtil;
import com.cyz.common.utils.R;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author 34928
* @description 针对表【study_resource_type】的数据库操作Service实现
* @createDate 2023-02-22 14:32:32
*/
@Service
public class StudyResourceTypeServiceImpl extends ServiceImpl<StudyResourceTypeMapper, StudyResourceType>
    implements StudyResourceTypeService {

    @Override
    public R queryStudyResourceTypes() {
        //查询数据
        List<StudyResourceType> list = list();
        //错误判断
        if(list.isEmpty()){
            return R.error(MyEnums.QUERY_FAIL);
        }
        return R.success(list);
    }

    @Override
    public R updateStudyResourceType(StudyResourceTypeDto studyResourceTypeDto) {
        //dto转换
        StudyResourceType studyResourceType = BeanCopyUtil.copyBean(studyResourceTypeDto, StudyResourceType.class);
        //更新数据
        boolean flag = updateById(studyResourceType);
        //错误判断
        if(flag){
            return queryStudyResourceTypes();
        }
        return R.error(MyEnums.UPDATE_FAIL);
    }

    @Override
    public R addStudyResourceType(StudyResourceTypeDto studyResourceTypeDto) {
        //dto转换
        StudyResourceType studyResourceType = BeanCopyUtil.copyBean(studyResourceTypeDto, StudyResourceType.class);
        //更新数据
        boolean flag = save(studyResourceType);
        //错误判断
        if(flag){
            return queryStudyResourceTypes();
        }
        return R.error(MyEnums.ADD_FAIL);
    }

    @Override
    public R deleteStudyResourceTypes(Integer[] ids) {
        //删除数据
        boolean flag = removeByIds(Arrays.asList(ids));
        //错误判断
        if(flag){
            return queryStudyResourceTypes();
        }
        return R.error(MyEnums.DELETE_FAIL);
    }
}




